/*
 * main.c
 *
 *  Created on: 2010-12-28
 *      Author: root
 */

#include "read_rules.h"
#include "command.h"
#include <gtk/gtk.h>


int ids_gtk_main(gint argc,gchar *argv[]);
char *readfile(FILE *fp,char flag);
char *select_rule(char *rules_line,char flag);
void select_row(GtkWidget *clist_rule,gint row,gint column,GdkEventButton *event,gpointer data);
void Add_a_rule();
void delete_a_rule();
void refresh_a_rule();


int number=0;
int row_number=0;
char rules[256][256];
int total=0;
char *tmp_rules;

GtkWidget *notebook;
GtkWidget *main_window;
GtkWidget *label_1,*label_2;
GtkWidget *box_1,*box_2;
GtkWidget *scrolled_1,*scrolled_2;
GtkWidget *clist_rule;
GtkWidget *add_rule_table;
GtkWidget *label_table_1,*label_table_2,*label_table_3,*label_table_4,*label_table_5,*label_table_6;
GtkWidget *entry_1,*entry_2,*entry_3,*entry_4,*entry_5,*entry_6;
GtkWidget *button_cancel_2,*button_cancel_1,*hbox_botton_2,*hbox_botton_1;
GtkWidget *button_add,*button_refresh,*button_delete;
gchar *titles[7]={"Number","SrcIP","DesIP","SrcPort","DesPort","Protocol","Information"};
int main(int argc, char *argv[])
{

	/* 命令标示符,同command.c；                   *
	 * 这里2头都进行了申明，如果申明在command.h中   *
	 * 会出现重复定义的错误，问题待解决                */
	  unsigned int SHOW_TCP = 0X01;
	  unsigned int SHOW_UDP = 0X02;
	  unsigned int do_print = 0X03;
	  unsigned int make_log   = 0X04;
	  unsigned int SHOW_ALL = 0X0F;
	  unsigned int ids      = 0X05;
//		printf(" Start to scan MAC......\n");
 	 init_arp();

    if(argc != 2)
    {
     fprintf(stdout, "usage: %s print/log/***\n", argv[0]);
    }else
    {
         if(strncmp(argv[1], "print", 5) == 0)
        {
        	do_command(do_print);
        }
         if(strncmp(argv[1], "log", 3) == 0)
        {
        	do_command(make_log);
        }
         if(strncmp(argv[1], "tcp", 3) == 0)
       	{
        	do_command(SHOW_TCP);
       	}
         if(strncmp(argv[1], "udp", 3) == 0)
       	{
        	do_command(SHOW_UDP);
       	}
         if(strncmp(argv[1], "all", 3) == 0)
       	{
      		do_command(SHOW_ALL);
       	}
         if(strncmp(argv[1], "ids", 3) == 0)
         {
        	 do_command(ids);
         }
         if(strncmp(argv[1],"gtk",3) == 0)
        {
        		gtk_init(&argc,&argv);
        		main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        		gtk_widget_set_usize(GTK_WIDGET(main_window),600,400);
        		gtk_window_set_title(GTK_WINDOW(main_window),"Intrusion Detection System");
        		//gtk_signal_connect(GTK_OBJECT(main_window),"destory",G_CALLBACK(gtk_main_quit),NULL);
        		gtk_widget_show(main_window);

        		notebook = gtk_notebook_new();
        		gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook),GTK_POS_LEFT);
        		gtk_container_add(GTK_CONTAINER(main_window),notebook);
        		gtk_widget_show(notebook);

        	//
        		box_1 = gtk_vbox_new(FALSE,0);
        		label_1 = gtk_label_new("1");
        		gtk_notebook_append_page(GTK_NOTEBOOK(notebook),box_1,label_1);
        		gtk_widget_show(label_1);
        		gtk_widget_show(box_1);

        		scrolled_1 = gtk_scrolled_window_new(NULL,NULL);
        	   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_1),
        			   GTK_POLICY_AUTOMATIC,GTK_POLICY_ALWAYS);
        		gtk_box_pack_start(GTK_BOX(box_1),scrolled_1,TRUE,TRUE,0);
        		gtk_widget_show(scrolled_1);

        		clist_rule=gtk_clist_new_with_titles(7,titles);
        		gtk_container_add(GTK_CONTAINER(scrolled_1),clist_rule);
        	   gtk_signal_connect(GTK_OBJECT(clist_rule),"select_row",
        					   GTK_SIGNAL_FUNC(select_row),NULL);
        		gtk_widget_show(clist_rule);

        		hbox_botton_1=gtk_hbox_new(FALSE,0);
        	   gtk_box_pack_start(GTK_BOX(box_1),hbox_botton_1,FALSE,TRUE,0);
        	   gtk_widget_show(hbox_botton_1);

        	   button_refresh=gtk_button_new_with_label("refresh");
        	   button_delete=gtk_button_new_with_label("delete");
        	   button_cancel_1=gtk_button_new_with_label("Cancel");
        	   gtk_box_pack_start(GTK_BOX(hbox_botton_1),button_refresh,TRUE,TRUE,0);
        	   gtk_box_pack_start(GTK_BOX(hbox_botton_1),button_delete,TRUE,TRUE,0);
        	   gtk_box_pack_start(GTK_BOX(hbox_botton_1),button_cancel_1,TRUE,TRUE,0);
        	   gtk_signal_connect_object(GTK_OBJECT(button_refresh),"clicked",
        			   GTK_SIGNAL_FUNC(refresh_a_rule),NULL);
        	   gtk_signal_connect_object(GTK_OBJECT(button_delete),"clicked",
        	   		   GTK_SIGNAL_FUNC(delete_a_rule),(gpointer) clist_rule);
        	   gtk_signal_connect(GTK_OBJECT(button_cancel_1),"clicked",
        			   G_CALLBACK(gtk_main_quit),NULL);
        	   gtk_widget_show(button_refresh);
        	   gtk_widget_show(button_delete);
        	   gtk_widget_show(button_cancel_1);


        		box_2 = gtk_vbox_new(FALSE,0);
        		label_2 = gtk_label_new("2");
        		gtk_notebook_append_page(GTK_NOTEBOOK(notebook),box_2,label_2);
        		gtk_widget_show(label_2);
        		gtk_widget_show(box_2);

        		scrolled_2 = gtk_scrolled_window_new(NULL,NULL);
        	   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_2),
        			   GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
        		gtk_box_pack_start(GTK_BOX(box_2),scrolled_2,TRUE,TRUE,0);
        		gtk_widget_show(scrolled_2);

        		add_rule_table=gtk_table_new(7,2,FALSE);
        		gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_2),add_rule_table);
        	   gtk_widget_show(add_rule_table);

        	   label_table_1=gtk_label_new("SrcIP");
        	   gtk_table_attach(GTK_TABLE(add_rule_table),label_table_1,0,1,0,1,GTK_FILL,GTK_FILL,0,0);
        	   entry_1=gtk_entry_new_with_max_length(36);
        	   gtk_object_set_data(GTK_OBJECT(scrolled_2),"entry_1",entry_1);
        	   gtk_entry_set_text(GTK_ENTRY(entry_1),"");
        	   gtk_editable_select_region(GTK_EDITABLE(entry_1),0,GTK_ENTRY(entry_1)->text_length);
        	   gtk_table_attach(GTK_TABLE(add_rule_table),entry_1,1,2,0,1,GTK_FILL,GTK_FILL,0,0);
        	   gtk_widget_show(label_table_1);
        	   gtk_widget_show(entry_1);

        	   label_table_2=gtk_label_new("DesIP");
        	   gtk_table_attach(GTK_TABLE(add_rule_table),label_table_2,0,1,1,2,GTK_FILL,GTK_FILL,0,0);
        	   entry_2=gtk_entry_new_with_max_length(36);
        	   gtk_object_set_data(GTK_OBJECT(scrolled_2),"entry_2",entry_2);
        	   gtk_entry_set_text(GTK_ENTRY(entry_2),"");
        	   gtk_editable_select_region(GTK_EDITABLE(entry_2),0,GTK_ENTRY(entry_2)->text_length);
        	   gtk_table_attach(GTK_TABLE(add_rule_table),entry_2,1,2,1,2,GTK_FILL,GTK_FILL,0,0);
        	   gtk_widget_show(label_table_2);
        	   gtk_widget_show(entry_2);

        	   label_table_3=gtk_label_new("SrcPort");
        	   gtk_table_attach(GTK_TABLE(add_rule_table),label_table_3,0,1,2,3,GTK_FILL,GTK_FILL,0,0);
        	   entry_3=gtk_entry_new_with_max_length(36);
        	   gtk_object_set_data(GTK_OBJECT(scrolled_2),"entry_3",entry_3);
        	   gtk_entry_set_text(GTK_ENTRY(entry_3),"");
        	   gtk_editable_select_region(GTK_EDITABLE(entry_3),0,GTK_ENTRY(entry_3)->text_length);
        	   gtk_table_attach(GTK_TABLE(add_rule_table),entry_3,1,2,2,3,GTK_FILL,GTK_FILL,0,0);
        	   gtk_widget_show(label_table_3);
        	   gtk_widget_show(entry_3);

        	   label_table_4=gtk_label_new("DesPort");
        	   gtk_table_attach(GTK_TABLE(add_rule_table),label_table_4,0,1,3,4,GTK_FILL,GTK_FILL,0,0);
        	   entry_4=gtk_entry_new_with_max_length(36);
        	   gtk_object_set_data(GTK_OBJECT(scrolled_2),"entry_4",entry_4);
        	   gtk_entry_set_text(GTK_ENTRY(entry_4),"");
        	   gtk_editable_select_region(GTK_EDITABLE(entry_4),0,GTK_ENTRY(entry_4)->text_length);
        	   gtk_table_attach(GTK_TABLE(add_rule_table),entry_4,1,2,3,4,GTK_FILL,GTK_FILL,0,0);
        	   gtk_widget_show(label_table_4);
        	   gtk_widget_show(entry_4);

        	   label_table_5=gtk_label_new("Protocol");
        	   gtk_table_attach(GTK_TABLE(add_rule_table),label_table_5,0,1,4,5,GTK_FILL,GTK_FILL,0,0);
        	   entry_5=gtk_entry_new_with_max_length(36);
        	   gtk_object_set_data(GTK_OBJECT(scrolled_2),"entry_5",entry_5);
        	   gtk_entry_set_text(GTK_ENTRY(entry_5),"");
        	   gtk_editable_select_region(GTK_EDITABLE(entry_5),0,GTK_ENTRY(entry_5)->text_length);
        	   gtk_table_attach(GTK_TABLE(add_rule_table),entry_5,1,2,4,5,GTK_FILL,GTK_FILL,0,0);
        	   gtk_widget_show(label_table_5);
        	   gtk_widget_show(entry_5);

        	   label_table_6=gtk_label_new("Information");
        	   gtk_table_attach(GTK_TABLE(add_rule_table),label_table_6,0,1,5,6,GTK_FILL,GTK_FILL,0,0);
        	   entry_6=gtk_entry_new_with_max_length(36);
        	   gtk_object_set_data(GTK_OBJECT(scrolled_2),"entry_6",entry_6);
        	   gtk_entry_set_text(GTK_ENTRY(entry_6),"");
        	   gtk_editable_select_region(GTK_EDITABLE(entry_6),0,GTK_ENTRY(entry_6)->text_length);
        	   gtk_table_attach(GTK_TABLE(add_rule_table),entry_6,1,2,5,6,GTK_FILL,GTK_FILL,0,0);
        	   gtk_widget_show(label_table_6);
        	   gtk_widget_show(entry_6);


        		hbox_botton_2=gtk_hbox_new(FALSE,0);
        	   gtk_box_pack_start(GTK_BOX(box_2),hbox_botton_2,FALSE,TRUE,0);
        	   gtk_widget_show(hbox_botton_2);

        	   button_add=gtk_button_new_with_label("Add");
        	   button_cancel_2=gtk_button_new_with_label("Cancel");
        	   gtk_box_pack_start(GTK_BOX(hbox_botton_2),button_add,TRUE,TRUE,0);
        	   gtk_box_pack_start(GTK_BOX(hbox_botton_2),button_cancel_2,TRUE,TRUE,0);
        	   gtk_signal_connect_object(GTK_OBJECT(button_add),"clicked",
        			   GTK_SIGNAL_FUNC(Add_a_rule),NULL);
        	   gtk_signal_connect(GTK_OBJECT(button_cancel_2),"clicked",
        			   G_CALLBACK(gtk_main_quit),NULL);
        	   gtk_widget_show(button_add);
        	   gtk_widget_show(button_cancel_2);



        	   gtk_main();


         }
         else{
//*************     现在这里是实验区      **************//
        	 printf("unknow command\n");
/*     		char xxxx[256];
     		sprintf(xxxx,"%s",read_information("rules",',',5));
     		printf("xxxx is %s\n",xxxx);
     		read_rules_from_file();
     		printf("value is %s %s %s %s %s %s",sip[0],sip[1],sip[2],sip[3],sip[4],sip[5]);*/
        	 int i=0;
        	 for(i=0;i<4;i++)
        	 {
        	 printf("arp is %x",fix_ipmac[1].ip_add[i]);
        	 }
//*************     现在这里是实验区      **************//
         }

    }
    free(*sip);
    return 1;
}

void select_row(GtkWidget *clist_rule,gint row,gint column,GdkEventButton *event,gpointer data)
{
	row_number = row;
}

void delete_a_rule()
{
    int row;
	 row=row_number;
	 gtk_clist_remove((GtkCList *)clist_rule,row);
	 number--;


     char *data;
     int i,j;
     FILE *s_rules;
     s_rules=fopen("rules","w");
     fclose(s_rules);
     s_rules=fopen("rules","a+");
     fputs("\n",s_rules);
		  for(i=0;i<total-2;i++)
		    {
			       for(j=1;j<7;j++)
			                   {
		               gtk_clist_get_text((GtkCList *)clist_rule,i,j,&data);
		               fputs(data,s_rules);
		               fprintf(s_rules,",");
			                  }
			       fprintf(s_rules,"\t\n");
		      }
		  fclose(s_rules);
}
void Add_a_rule()
{
	GtkWidget *OK_window;
	GtkWidget *OK_label;
	GtkWidget *OK_vbox;
	GtkWidget *OK_button;
	FILE *fd_rules;
	char *text[7];
	char num[7];
	int i=0;
	int j=0;

	sprintf(num,"%d",number);
	text[0]=num;
   text[1]=(char *)gtk_entry_get_text(GTK_ENTRY(entry_1));
   text[2]=(char *)gtk_entry_get_text(GTK_ENTRY(entry_2));
   text[3]=(char *)gtk_entry_get_text(GTK_ENTRY(entry_3));
   text[4]=(char *)gtk_entry_get_text(GTK_ENTRY(entry_4));
   text[5]=(char *)gtk_entry_get_text(GTK_ENTRY(entry_5));
   text[6]=(char *)gtk_entry_get_text(GTK_ENTRY(entry_6));
   for(j=1;j<7;j++)
   {
	   if(strlen(text[j])==0)
	   {
		   text[j]="any";
	   }
   }

   fd_rules=fopen("rules","a+");
   for(i=1;i<7;i++)
   {
	   fputs(text[i],fd_rules);
	   fputs(",",fd_rules);
   }
   fputs("\t\n",fd_rules);
   fclose(fd_rules);

   OK_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_widget_set_usize(GTK_WIDGET(OK_window),200,100);
   gtk_widget_show(OK_window);

   OK_vbox = gtk_vbox_new(FALSE,0);
   gtk_container_add(GTK_CONTAINER(OK_window),OK_vbox);
   gtk_widget_show(OK_vbox);

   OK_label = gtk_label_new("Add Successfully");
   OK_button = gtk_button_new_with_label("OK");
   gtk_box_pack_start(GTK_BOX(OK_vbox),OK_label,FALSE,FALSE,0);
   gtk_box_pack_start(GTK_BOX(OK_vbox),OK_button,FALSE,FALSE,0);
   gtk_signal_connect_object(GTK_OBJECT(OK_button),"clicked",
		   GTK_SIGNAL_FUNC(gtk_widget_destroy),GTK_OBJECT(OK_window));
   gtk_widget_show(OK_label);
   gtk_widget_show(OK_button);


   gtk_clist_append((GtkCList *)clist_rule,text);
   gtk_entry_set_text(GTK_ENTRY(entry_1),"");
   gtk_entry_set_text(GTK_ENTRY(entry_2),"");
   gtk_entry_set_text(GTK_ENTRY(entry_3),"");
   gtk_entry_set_text(GTK_ENTRY(entry_4),"");
   gtk_entry_set_text(GTK_ENTRY(entry_5),"");
   gtk_entry_set_text(GTK_ENTRY(entry_6),"");
   number++;

}

char *select_rule(char *rules_line,char flag)
{
	int i = 0;
	int j = 0;
	char tmp_sr[256];
	char *rt_line;

	for(j=0;(rules_line[j]&&rules_line[j]!=flag);j++)
		tmp_sr[j] = rules_line[j];
	tmp_sr[j] = '\0';

	while(rules_line[j]==flag)
		++j;

	while((rules_line[i++]=rules_line[j++]))
		rt_line=tmp_sr;
	return rt_line;
}

char *readfile(FILE *fp,char flag)
{
	char word[256];
	char *string;
	int x=0;
	while(1)
	{
		word[x]=(char)fgetc(fp);
		if((word[x]==flag)||(feof(fp)))
		{
			if(word[x]!=flag)
				x++;
			word[x]='\0';
			string=word;
			return string;
		}
		++x;
	}
}

void refresh_a_rule()
{

	FILE *fp;
	fp = fopen("rules","r+");

	total = 0;

	while(!feof(fp))
	{
		sprintf(rules[total],"%s",readfile(fp,'\n'));
		total++;
	}
	--total;
	fclose(fp);



	char rnum[7];
	char *empty_text[7]={"","","","","","",""};
	char *rules_text[7];
   char try[512];


	int m,n;
	gtk_clist_clear((GtkCList *)clist_rule);
	for(m=1;m<total;m++)
	{
		gtk_clist_append((GtkCList *)clist_rule,empty_text);
		tmp_rules=rules[m];
		printf("%s\n",tmp_rules);
		sprintf(rnum,"%d",m);
		rules_text[0]=rnum;
		gtk_clist_set_text((GtkCList *) clist_rule,m-1,0,rules_text[0]);
		for(n=1;n<7;n++)
		{
			sprintf(try,"%s",select_rule(tmp_rules,','));
			rules_text[n]=try;
		   gtk_clist_set_text((GtkCList *) clist_rule,m-1,n,rules_text[n]);
		}
	}
}
